package com.wanxing.sellerapp.device

import android.view.KeyEvent
import java.lang.StringBuilder

/**
 * 读取芯片
 */
class KeyboardReader(val onScan: (String)->Unit) {

    private val cache = arrayListOf<Int>()

    private var holdTimeStart = 0L

    fun feed(keyCode: Int){
        if (keyCode == KeyEvent.KEYCODE_ENTER) {
            val now = System.currentTimeMillis()
            if (now - holdTimeStart >= 1000) {
                holdTimeStart = now
                onScan(parseData(dataList = cache))
            }
            cache.clear()
        } else {
            cache.add(keyCode)
        }
    }


    private fun parseData(dataList: List<Int>): String {
        val sb = StringBuilder()
        var shift = false //大小写标记
        dataList.forEach {
            if (it == KeyEvent.KEYCODE_SHIFT_LEFT) {
                shift = true //开启大写标记
            } else {
                var ch: Char? = null
                when (it) {
                    KeyEvent.KEYCODE_0,
                    KeyEvent.KEYCODE_1,
                    KeyEvent.KEYCODE_2,
                    KeyEvent.KEYCODE_3,
                    KeyEvent.KEYCODE_4,
                    KeyEvent.KEYCODE_5,
                    KeyEvent.KEYCODE_6,
                    KeyEvent.KEYCODE_7,
                    KeyEvent.KEYCODE_8,
                    KeyEvent.KEYCODE_9 -> {
                        if (shift) {
                            when (it) {
                                KeyEvent.KEYCODE_1 -> ch = '!'
                                KeyEvent.KEYCODE_2 -> ch = '@'
                                KeyEvent.KEYCODE_3 -> ch = '#'
                                KeyEvent.KEYCODE_4 -> ch = '$'
                                KeyEvent.KEYCODE_5 -> ch = '%'
                                KeyEvent.KEYCODE_6 -> ch = '^'
                                KeyEvent.KEYCODE_7 -> ch = '&'
                                KeyEvent.KEYCODE_8 -> ch = '*'
                                KeyEvent.KEYCODE_9 -> ch = '('
                                KeyEvent.KEYCODE_0 -> ch = ')'
                            }
                        } else ch = (it - KeyEvent.KEYCODE_0 + '0'.code).toChar()
                    }
                    KeyEvent.KEYCODE_A,
                    KeyEvent.KEYCODE_B,
                    KeyEvent.KEYCODE_C,
                    KeyEvent.KEYCODE_D,
                    KeyEvent.KEYCODE_E,
                    KeyEvent.KEYCODE_F,
                    KeyEvent.KEYCODE_G,
                    KeyEvent.KEYCODE_H,
                    KeyEvent.KEYCODE_I,
                    KeyEvent.KEYCODE_J,
                    KeyEvent.KEYCODE_K,
                    KeyEvent.KEYCODE_L,
                    KeyEvent.KEYCODE_M,
                    KeyEvent.KEYCODE_N,
                    KeyEvent.KEYCODE_O,
                    KeyEvent.KEYCODE_P,
                    KeyEvent.KEYCODE_Q,
                    KeyEvent.KEYCODE_R,
                    KeyEvent.KEYCODE_S,
                    KeyEvent.KEYCODE_T,
                    KeyEvent.KEYCODE_U,
                    KeyEvent.KEYCODE_V,
                    KeyEvent.KEYCODE_W,
                    KeyEvent.KEYCODE_X,
                    KeyEvent.KEYCODE_Y,
                    KeyEvent.KEYCODE_Z -> {
                        ch = if (shift)
                            (it - KeyEvent.KEYCODE_A + 'A'.toInt()).toChar()
                        else
                            (it - KeyEvent.KEYCODE_A + 'a'.toInt()).toChar()
                    }
                    KeyEvent.KEYCODE_EQUALS -> ch = if (shift) '+' else '='
                    KeyEvent.KEYCODE_PLUS -> ch = '+'
                    KeyEvent.KEYCODE_MINUS ->  ch = if (shift) '_' else '-'
                    KeyEvent.KEYCODE_NUMPAD_MULTIPLY -> ch = '*'
                    KeyEvent.KEYCODE_NUMPAD_DIVIDE -> ch = if (shift) '?' else '/'
                    KeyEvent.KEYCODE_GRAVE -> ch = if (shift) '~' else '`'
                    KeyEvent.KEYCODE_BACKSLASH -> ch = if (shift) '|' else '\\'
                    KeyEvent.KEYCODE_COMMA -> ch = if (shift) '<' else ','
                    KeyEvent.KEYCODE_PERIOD -> ch = if (shift) '>' else '.'
                    KeyEvent.KEYCODE_SEMICOLON -> ch = if (shift) ':' else ';'
                    KeyEvent.KEYCODE_APOSTROPHE -> ch = if (shift) '"' else '\''
                }
                ch?.let { sb.append(it) }
                shift = false //关闭大写标记
            }
        }
        return sb.toString()
    }
}